﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GuiZhouSXNB.ServiceReference1;

namespace MediSIIM.BizComponent.GuiZhouSXNB
{
    public class GuiZhouSXNBTradeParms
    {
        public HisInterfacePortTypeClient HisClient = new HisInterfacePortTypeClient();
        public System.Reflection.ParameterInfo[] Params { get; set; }
        public object OutData { get; set; }
        /// <summary>
        /// 此接口的方法名称
        /// </summary>
        public string MethodName { get; set; }
        public List<object> Args { get; set; }
        public object this[int index]
        {
            get { return Args[index]; }
            set
            {
                if (index < Args.Count)
                {
                    Args[index] = value;
                }
                else
                {
                    Args.Add(value);
                }
            }
        }

        public object this[int x, int y]
        {
            set
            {
                if (Args.Count < Params.Length)
                {
                    Args.Add(Activator.CreateInstance(Params[x].ParameterType));
                }
                if (Params[x].ParameterType == typeof(ArrayOfString))
                {
                    ((dynamic)Args[x]).Add(value.ToString());
                }
                else
                {
                    ((dynamic)Args[x]).Add(float.Parse(value.ToString()));
                }
            }
        }

        public GuiZhouSXNBTradeParms()
        {

        }

        public GuiZhouSXNBTradeParms(string methodname)
        {
            MethodName = methodname;
            Args = new List<object>();
            Params = typeof(HisInterfacePortType).GetMethod(MethodName).GetParameters();
        }

        public string GetInput(string p)
        {
            string str = "";
            foreach (var item in Args)
            {
                str += item + p;
            }
            return str;
        }

        public  GuiZhouSXNBTradeParms YBCall(GuiZhouSXNBTradeParms callparam)
        {
            try
            {
                callparam.OutData = HisClient.GetType().GetMethod(callparam.MethodName).Invoke(HisClient, callparam.Args.ToArray());
                if (callparam.OutData.GetType() == typeof(string) && callparam.OutData.ToString().StartsWith("SYSTEM"))
                {
                    if (callparam.OutData.ToString() == "SYSTEM_0000")
                    {
                    }
                    else
                    {
                        throw new Exception(ErrorMsg.GetErrorMessage(callparam.OutData.ToString()));
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex.InnerException ?? ex;
            }
            return callparam;
        }
    }
}
